package com.xnew.utils;

import com.xnew.exception.CustomException;

import java.io.Serializable;
import java.util.Map;

/**
 * @Author anonymous
 * @Date 2020/10/14
 */
public class DataInput<T> implements Serializable {

    /**
     * 默认页码
     */
    private Integer pageNum = 1;

    /**
     * 默认每页数据条数
     */
    private Integer pageSize = 10;

    /**
     * 模糊搜索内容
     */
    private String searchText;

    /**
     * 排序字段
     */
    private String sortField;

    /**
     * 升序/降序
     */
    private String sortOrder;

    /**
     * 传入数据
     */
    private T data;

    /**
     * 精确查询条件
     */
    private Map<String, Object> param;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }
    public Integer getStart()
    {
        return (pageNum-1) * pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, Object> getParam() {
        return param;
    }


    public void setParam(Map<String, Object> param) {
        this.param = param;
    }


    /**
     * 获取参数
     * @param key
     * @param <S>
     * @return
     */
    public Object getParamValue(String key,Object def)
    {
        //如果没有参数 返回默认或者报错
        if(param==null){
            if(def!=null)
            {
                return  def;
            }
            throw  new CustomException(500,"获取参数失败"+key);
        }
        //没找到也是一样
        Object o = param.get(key);
        if(o==null)
        {
            if(def!=null)
            {
                return  def;
            }
            throw  new CustomException(500,"获取参数失败"+key);
        }
        return o;
    }

    /**
     * 返回参数值
     * @param key
     * @param <S>
     * @return
     */
    public Object getParamValue(String key){
        return  getParamValue(key,null);
    }

    /**
     * 返回参数值
     * @param key
     * @param <S>
     * @return
     */
    public String getParamStringValue(String key){
        return (String) getParamValue(key,null);
    }
    /**
     * 返回参数值
     * @param key
     * @param <S>
     * @return
     */
    public String getParamStringValue(String key,String def){
        return (String) getParamValue(key,def);
    }



    @Override
    public String toString() {
        return "DataInput{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", searchText='" + searchText + '\'' +
                ", sortField='" + sortField + '\'' +
                ", sortOrder='" + sortOrder + '\'' +
                ", data=" + data +
                ", param=" + param +
                '}';
    }
}